package com.moon;

import com.moon.utils.FileUtils;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class AutoCoder {
    //四大累赘
    private static String javaPath = "";
    private static String daoPack = "";
    private static String daoPath = "";
    private static String daoBakPath = "";

    //给四大累赘赋值
    static {
        Properties p = new Properties();
        try {
            p.load(AutoCoder.class.getClassLoader().getResourceAsStream("AutoCoder.properties"));
            javaPath = p.getProperty("autocoder.path.java");
            daoPack = p.getProperty("autocoder.pack.dao");
            daoPath = javaPath + daoPack.replaceAll("\\.", "\\\\");
            daoBakPath = javaPath + "com\\moon\\daobak\\";
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 入口
    public static void main(String[] args) throws IOException, XMLParserException, SQLException, InterruptedException, InvalidConfigurationException {
        makeDaoDir();
        daoToDaoBak();
        mbg();
        daoBakToDao();
        delDaoBak();
        System.out.println("逆向工程完成 : )");
    }

    //创建dao文件夹(已存在则不创建)
    public static void makeDaoDir() {
        File daoDir = new File(daoPath);
        if (!daoDir.exists()) {
            daoDir.mkdirs();
        }
    }

    //mybatis逆向工程
    public static void mbg() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        Configuration config = new ConfigurationParser(warnings)
                .parseConfiguration(AutoCoder.class.getClassLoader().getResourceAsStream("AutoCoder.xml"));
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, new DefaultShellCallback(true), warnings);
        myBatisGenerator.generate(null);
    }

    //备份当前dao到daobak
    public static void daoToDaoBak() {
        File daoDir = new File(daoPath);
        File[] fileArr = daoDir.listFiles();
        //创建备份文件夹
        File daoBakDir = new File(daoBakPath);
        daoBakDir.mkdirs();
        //备份
        for (File file : fileArr) {
            FileUtils.copy(file.getAbsolutePath(), daoBakPath + file.getName());
        }
    }

    //还原daobak到dao
    public static void daoBakToDao() {
        //还原
        File daoBakDir = new File(daoBakPath);
        File[] fileArr = daoBakDir.listFiles();
        for (File file : fileArr) {
            FileUtils.copy(file.getAbsolutePath(), daoPath + "\\" + file.getName());
        }
    }

    //删除daobak备份
    public static void delDaoBak() {
        File daoBakDir = new File(daoBakPath);
        File[] fileArr = daoBakDir.listFiles();
        for (File file : fileArr) {
            file.delete();
        }
        daoBakDir.delete();
    }

    //
}